package dk.itu.KrakMap;

import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.*;

public class MapController {

    private final KrakMap map;
    private final MapView view;
    private final MainWindow window;
    private ZoomFilter filter;

    MapController(final KrakMap map, final MapView view, final MainWindow window) {
        this.map = map;
        this.view = view;
        this.view.setController(this);
        this.window = window;
        this.view.addViewportListener(new ViewportChangedListener() {

            public void ViewportChanged() {
                filter = ZoomFilter.getFromPixelsPerMeter(view.getPixelPerMeter());
                Viewport viewport = view.getViewport().clone();
                double ppm = view.getPixelPerMeter();
                if (ppm >= 1.5) {
                    viewport.zoom(-300);
                    
                } else if (ppm >= 1) {
                    viewport.zoom(-200);
                    
                } else if (ppm >= 0.3) {
                    
                    viewport.zoom(-50);
                }

                view.setRoads(map.getRoads(viewport, filter));
            }
        });
        Viewport startViewport = this.map.getMaxViewport();
        startViewport.zoom(-10);
        this.view.showViewport(startViewport);

        //view.setAllRoads(map.getRoads(map.getMaxViewport(), ZoomFilter.getMostOpenFilter()));

        this.view.addRoutePlanRequestListener(new RoutePlanRequestListener() {

            public void RoutePlanRequested(RoutePlanRequest req) {
                RoutePlan route = map.getRoute(req);
                if (route != null) {
                    view.setRoutePlan(route);
                    window.setStatusText(route.getFormattedlength());
                } else {
                    view.removeRoutePlan();
                    setStatusText("<html><font color=red>Der kunne ikke findes nogen gyldig rute!</font>"
                            + "<br><i>Prøv at skifte befordringsmiddel eller start-/slutpunkt</i>"
                            + "</html>");
                }
            }
        });

        this.view.addMapMouseEventListener(new MapMouseEventListener() {

            public void mouseMoved(MapPoint pos) {
                window.setRoadLabel(map.getNearestEdgeName(pos, filter));
            }
        });
    }

    public void resetMap() {
        this.view.showViewport(this.map.getMaxViewport());
    }

    KrakNode getNode(MapPoint point) {
        //We want to snap the choosen points to the nearest node WHICH IS ON
        //the nearest edge. Just taking the nearest node leads to confussion
        //when clicking on a longer edge..
        KrakEdge e = map.getNearestEdge(point);
        return point.dist(e.v1.getMapPoint()) < point.dist(e.v2.getMapPoint()) ? e.v1 : e.v2;
    }

    public void setStatusText(String text) {
        window.setStatusText(text);
    }
}
